import requests
from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

# 1.转类型 默认使用lxml的解析库
soup = BeautifulSoup(html_doc, 'html.parser')
# soup = BeautifulSoup(html_doc,'html.parser').prettify()
# print(soup)
# print(type(soup))  #<class 'bs4.BeautifulSoup'>

# 2.格式化输出
# result = soup.prettify()
# print(result)

# 3.解析数据
# result = soup.head
# result = soup.p  #只能获取了第一个
# result = soup.a    #只能获取了第一个
# print(result)
# print(type(result))  #<class 'bs4.element.Tag'>

# 取内容 文本
result = soup.a.string
# print(result)   #Elsie
# print(type(result))   #<class 'bs4.element.NavigableString'>

# 去属性
result = soup.a['href']
# print(result)  #http://example.com/elsie

# 四大对象：
# 1.Tag
# 2.NavigableString
# 3.BeautifulSoup
# 4.Comment :注释的内容
